<?php
namespace core\execute\method;

use core\Object;
class MethodExecute implements Object{

	public static function executeAll(array $methods){
		foreach ($methods as $method){
			if($method instanceof Method){
				static::execute($method);
			}
		}
	}

	public static function execute(Method $method){
		$class = new \ReflectionClass($method);

		$propertyObject = $class->getProperty(Method::$PROPERTY_OBJECT);
		$propertyObject->setAccessible(true);

		$propertyName = $class->getProperty(Method::$PROPERTY_NAME);
		$propertyName->setAccessible(true);

		$propertyParameters = $class->getProperty(Method::$PROPERTY_PARAMETERS);
		$propertyParameters->setAccessible(true);

		$propertyIsPublic = $class->getProperty(Method::$PROPERTY_IS_PUBLIC);
		$propertyIsPublic->setAccessible(true);

		$class = new \ReflectionClass($propertyObject->getValue($method));
		if($class->hasMethod($propertyName->getValue($method))){
			$methodExecute = $class->getMethod($propertyName->getValue($method));
			if(!$methodExecute->isPublic() && $propertyIsPublic->getValue($method)){
				throw new \Exception("Erro: Inaccessible Method");
			}

			$methodExecute->setAccessible(true);
			return $methodExecute->invokeArgs($propertyObject->getValue($method), $propertyParameters->getValue($method));
		} else {
			throw new \Exception("Erro: Invalid Method");
		}
	}
}
?>